package com.yunbiji.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yunbiji.controller.CalendarController;
/**
 * Calendar模型类
 * @author 何瑞
 *
 */
public class Calendar {
	/**
	 * 添加事件到数据库
	 * @param calendar
	 * @return
	 */
	public boolean add(CalendarController calendar) {
		String sql = "insert into calendar(title, start, end, allday, color,tel) values(?, ?, ?, ?, ?,?)";
		
		int i=Db.update(sql,calendar.getTitle(),calendar.getStart(),calendar.getEnd(),calendar.getAllday(),calendar.getColor(),calendar.getTel());
		if(i==1)
			return true;
		else
			return false;
	}
	/**
	 * 从数据库删除事件
	 * @param id
	 * @return
	 */
	public boolean del(Integer id) {
		boolean i=Db.deleteById("calendar", id);
		return i;
	}

/**
 * 根据所点击的时间从数据库调取详细信息
 * @param id
 * @return
 */
	public CalendarController findById(Integer id){
		CalendarController calendar=new CalendarController();
		Record record=Db.findFirst("select * from calendar where id=?",id);
		calendar.setId(record.getInt("id"));
		calendar.setAllDay(record.getInt("allday"));
		calendar.setTitle(record.getStr("title"));
		calendar.setStart(record.getStr("start"));
		calendar.setEnd(record.getStr("end"));
		calendar.setAllDay(record.getInt("allday"));
		calendar.setColor(record.getStr("color"));
		return calendar;
	}
	/**
	 * 更新一个日程事件
	 * @param calendar
	 * @return
	 */
	public boolean modify(CalendarController calendar) {
		String sql = "update calendar set title = ?, start = ?, end = ?, allDay = ?, color = ? ,tel=?where id = ?";
		int i=Db.update(sql,calendar.getTitle(),calendar.getStart(),calendar.getEnd(),calendar.getAllday(),calendar.getColor(),calendar.getTel(),calendar.getId());
		if(i==1)
			return true;
		else
			return false;
	}
}
